# -*- coding: utf-8 -*-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from werkzeug.utils import import_string


from config import config, BASE_PATH
from utils.logger import set_root_logger

# db = SQLAlchemy(session_options={"expire_on_commit": False})
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'


def create_app(config_name=None):
    app = Flask(__name__)
    if config_name is not None:
        app.config.from_object(config[config_name])
    app.base_path = BASE_PATH

    db.init_app(app)
    login_manager.init_app(app)

    installed_blueprints = app.config.get('INSTALLED_BLUEPRINTS', [])
    for bp in installed_blueprints:
        bp = import_string(bp)
        app.register_blueprint(bp)

    # set_root_logger()

    app.jinja_env.variable_start_string = '{{ '
    app.jinja_env.variable_end_string = ' }}'

    return app
